<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>选号码</title>
  <link rel="shortcut icon" href="/favicon.ico"/>
  <link rel="stylesheet" href="css/base.css">
  <link rel="stylesheet" href="css/common.css">
  <link rel="stylesheet" href="css/index.css">
  <link rel="stylesheet" href="css/Phone.css">
</head>
<body>
<div class="container">
  <h3 class="title">选择号码</h3>
  <div class="public-con gff phone ">
    <div class="child child-border">
      <label class="titles">套餐方案</label>
      <div class="plan">畅玩卡1-39元/月</div>
    </div>
    <div class="child">
      <label class="titles" >选择号码</label>
      <input id="phoneNumber" class="ipt" type="text"   maxlength="11" placeholder="请输入手机号"  />
    </div>
  </div>
<h3 class="title">入网信息</h3>
<div class="public-con gff message ">
  <div class="child child-border">
    <label class="titles" >机主姓名</label>
    <input id="userName" class="ipt" type="text"   minlength="2" placeholder="请输入身份证姓名"  />
  </div>
  <div class="child">
    <label class="titles">身份证号</label>
    <input id="idCardNumber" class="ipt" type="text"   maxlength="18" placeholder="请输入身份证号"  />
  </div>
</div>

<h3 class="title">收货信息</h3>
<div class="public-con gff address ">
  <div class="child child-border">
    <label class="titles">联系电话</label>
    <input class="ipt" id="phone"  type="" maxlength="11" placeholder="请输入收货人手机号" />
  </div>

  <div class="child">
    <label class="titles">详细地址</label>
    <input class="ipt"  type="text" placeholder="省份+市区+街道/镇＋村/小区/写字楼＋门牌号"  />
  </div>
<div class="btn1">
  <div class="free active" onclick="">确认办理</div>
</div>
</div>
</div>
<script>
  document.querySelector(".btn1").addEventListener("click", function() {
    event.preventDefault();

    var newPackageId = 1;
    var phoneNumber = document.getElementById("phoneNumber").value;
    var userName = document.getElementById("userName").value;
    var idCardNumber = document.getElementById("idCardNumber").value;
    var phoneRegex = /^\d{11}$/;
    if (!phoneRegex.test(phoneNumber)) {
      alert('手机号必须是11位数字！');
      return;
    }

    var data = {Package_ID: newPackageId, phoneNumber: phoneNumber, User_Name: userName,ID_Card_Number:idCardNumber };

    fetch('/phone', {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
            .then(response => {
              if (!response.ok) {
                throw new Error('Network response was not ok');
              }
              alert('开户成功！');
            })
            .catch(error => {
              console.error('Error:', error);
              alert('充值失败，请稍后再试！');
            });
  });
</script>
</body>
</html>